package com.zbbt.sportslottery.ui.adapter;

import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.bean.FeedBackData;
import com.zbbt.sportslottery.ui.widget.CircleImageView;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.TimeUtil;

public class FeedBackListAdapter extends BaseAdapter {
	private List<FeedBackData> feedBackContent;
	private Context context;
	private LayoutInflater mInflater;
	private int currentType;

	public FeedBackListAdapter(List<FeedBackData> feedBackContent, Context context) {
		this.feedBackContent = feedBackContent;
		this.context = context;
	}

	@Override
	public boolean isEnabled(int position) {
		return false;
	}

	@Override
	public int getCount() {
		return feedBackContent.size();
	}

	@Override
	public Object getItem(int position) {
		return feedBackContent.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return feedBackContent.get(position).getFlag() - 1;
	}

	@Override
	public int getViewTypeCount() {
		// 共有两个不同的布局
		return 2;
	}

	@SuppressWarnings("unused")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View userView = null;
		View serveView = null;
		FeedBackData data = feedBackContent.get(position);
		String Lmcontent = data.getContent();// 留言内容
		mInflater = LayoutInflater.from(context);
		currentType = getItemViewType(position);
		if (data != null) {
			switch (currentType) {
			case 0:

				RightUser leftUser = null;
				if (userView == null) {
					userView = mInflater.inflate(R.layout.item_list_feed_back_right, null);
					leftUser = new RightUser();
					leftUser.userimage = (CircleImageView) userView.findViewById(R.id.user_img_right);
					leftUser.user = (TextView) userView.findViewById(R.id.user_name_right);
					leftUser.userContent = (TextView) userView.findViewById(R.id.user_content_right);
					leftUser.userContentTime = (TextView) userView.findViewById(R.id.user_time_right);
					userView.setTag(leftUser);
				} else {
					leftUser = (RightUser) userView.getTag();
				}
				int userId = data.getUserId();
				if ("0".equals(userId) || App.isLogin==false) {
					leftUser.user.setText("匿名");
				} else {
					leftUser.user.setText(SharePreferUtil.getString(Constant.USER_NAME, ""));
				}
				if (Constant.USER_PHOTO_IMG.equals("")) {
					leftUser.userimage.setImageDrawable(context.getResources().getDrawable(R.drawable.kehu_top));
				} else {
					String userImg = SharePreferUtil.getString(Constant.USER_PHOTO_IMG, "");
					settingImageViewChange(userImg,leftUser.userimage);
				}

				if (data.getCreateAt() != null) {
					String strtimer = TimeUtil.getTimeInterval(data.getCreateAt());
					leftUser.userContentTime.setText(strtimer);
				}
				leftUser.userContent.setText(Lmcontent);// 提问
				convertView = userView;
				break;
			// 服务端的----------------------------------------------------------------------------------------------------
			case 1:
				LeftServe leftServe = null;
				if (serveView == null) {
					serveView = mInflater.inflate(R.layout.item_list_feed_back_left, null);
					leftServe = new LeftServe();
					leftServe.serve = (TextView) serveView.findViewById(R.id.service_name_left);
					leftServe.serveContent = (TextView) serveView.findViewById(R.id.service_content_left);
					leftServe.serveContentTime = (TextView) serveView.findViewById(R.id.service_time_right);
					serveView.setTag(leftServe);
				} else {
					leftServe = (LeftServe) serveView.getTag();
				}
				if (data.getCreateAt() != null) {
					String strtimer1 = TimeUtil.getTimeInterval(data.getCreateAt());
					leftServe.serveContentTime.setText(strtimer1);
				}
				leftServe.serveContent.setText(Lmcontent);
				convertView = serveView;
				break;

			default:
				break;
			}
		}
		return convertView;
	}
    /**
     * 设置头像
     * @param userimage 
     */
	private void settingImageViewChange(String userImg, CircleImageView userimage) {
		ImageLoader.getInstance().displayImage(SharePreferUtil.getString(Constant.USER_PHOTO_IMG, "0"), userimage, App.option, new ImageLoadingListener() {
			@Override
			public void onLoadingStarted(String imageUri, View view) {}

			@Override
			public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
				((CircleImageView) view).setImageDrawable((context.getResources().getDrawable(R.drawable.kehu_top)));
			}

			@Override
			public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {}

			@Override
			public void onLoadingCancelled(String imageUri, View view) {}
		});
      
	}

	class RightUser {
		TextView user;
		TextView userContent;
		TextView userContentTime;
		CircleImageView userimage;
	}

	class LeftServe {
		TextView serve;
		TextView serveContent;
		TextView serveContentTime;
	}

	public FeedBackListAdapter() {
		super();
	}

}
